/**************************************************************************************
* MessageBuffer.java
* 
* Stores messages
* 
*************************************************************************************/
package creid.mythos.engine;

//import java.io.File;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.Calendar;

import creid.mythos.shared.GuiInterface;

public class MessageBuffer
{
	//-----------------------------------------------------------------
	// Constants
	//-----------------------------------------------------------------

	//How many old messages we will save
	public static final int MAX_MSG_HISTORY = 32;
	
	//-----------------------------------------------------------------
	// Attributes
	//-----------------------------------------------------------------
	
	private String[] history;
	
	private int first;

	private int next;
	private int previous;
	

	//Handle to the UI for writing messages to the screen
	private GuiInterface gui;
	
	//Error log
//	private String eFile;
	
	//-----------------------------------------------------------------
	// Constructors
	//-----------------------------------------------------------------
	
	public MessageBuffer()
	{
		history = new String[MAX_MSG_HISTORY];
		
		clearBuffer();
		setGui(null);
		
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
//		eFile = Mythos.getSaveDirectoryPath() + File.separator + "error." + sdf.format(Calendar.getInstance().getTime()) + ".log";
//		
//		try
//		{
//			File f = new File(eFile);
//			if (!f.exists())
//				f.createNewFile();
//		} catch (IOException e)
//		{
//			e.printStackTrace();
//		}
	}

	//-----------------------------------------------------------------
	// logMessage: log a message
	//-----------------------------------------------------------------

	public void setGui(GuiInterface gui)
	{
		this.gui = gui;
	}
	
	private boolean matchesPrevious(String check)
	{
		if (history[previous] == null)
			return false;
		
		String temp = history[previous];
		temp = temp.replaceFirst(" \\(x[0-9]+\\)$", "");
		
		if (temp.equals(check))
			return true;
		else
			return false;
	}
	
	private int getCount(String msg)
	{
		String temp = history[previous];
		temp = temp.replaceFirst("[^\\(]+\\(x", "");
		temp = temp.replaceFirst("\\)", "");
		
		int count = 1;
		
		try
		{
			count = Integer.parseInt(temp);
		} catch	(NumberFormatException ex) {}
		
		return count;
	}
	
	public void logMessage(String message)
	{
		//Combine messages if there is a match
		if (matchesPrevious(message))
		{
			int count = getCount(history[previous]) + 1;
			message += " (x" + count + ")";
			history[previous] = message;
			
			if (gui != null)
				gui.overwriteLastMessage(message);
		}
		else
		{
			history[next] = message;
			
			if (next + 1 == first)
				if (first < history.length - 1)
					first++;
				else
					first = 0;
			
			previous = next;
			
			if (next < history.length - 1)
				next++;
			else
				next = 0;
			
			//Print to screen
			if (gui != null)
				gui.showMessage(message);
		}
	}
	
	public void logTransientMessage(String message)
	{
		if(gui != null)
			gui.showTransientMessage(message);
	}
	
	public void clearBuffer()
	{
		for (int i = 0; i < history.length; i++)
			history[i] = "";
		
		first = 0;
		next = 0;
		previous = history.length - 1;
		
		//Clear the gui if we have one
		if (gui != null)
			gui.clearMessages();
	}
	
	public String[] getHistory()
	{
		return history;
	}
	
	public void errorLog(String message)
	{
		System.out.println(message);
		logMessage("ERROR:" + message);
		
//		FileWriter errLog = null; 
//		
//		try
//		{
//			errLog = new FileWriter(eFile, true);
//			errLog.write(message + System.getProperty("line.separator"));
//		} catch (IOException e)
//		{
//			e.printStackTrace();
//		}
//		finally
//		{
//			try
//			{
//			errLog.close();
//			}
//			catch(IOException ex)
//			{
//				ex.printStackTrace();
//			}
//		}
	}
}
